package cn.edu.scau.cmi.ema.controller;

import java.util.Set;
import java.util.List;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.service.*;
import cn.edu.scau.cmi.ema.service.pagination.*;
import cn.edu.scau.cmi.ema.controller.base.CmiEmaController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;

@Controller("FoodtypeController")
public class FoodtypeController extends CmiEmaController {

	@RequestMapping("/listFoodtypes")
	public ModelAndView listFoodtypes(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = foodtypePaginationService.updatePagination(request, pagination);
		Set<Foodtype> foodtypes = foodtypePaginationService.getPaginationFoodtypes(pagination);

		mav.addObject("foodtypes", foodtypes);
		mav.addObject("entityName", "foodtype");
		mav.addObject("pagination", pagination);
		mav.setViewName("foodtype/listFoodtypes.jsp");
		return mav;
	}

	public String listFoodtypes() {
		return "redirect:/listFoodtypes";
	}

	@RequestMapping("/viewFoodtype")
	public ModelAndView viewFoodtype(@RequestParam Integer foodtypeId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("foodtype", foodtypeDAO.findFoodtypeByPrimaryKey(foodtypeId));
		mav.setViewName("foodtype/viewFoodtype.jsp");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteFoodtypes")
	public ModelAndView confirmDeleteFoodtypes(
			@RequestParam(value = "deleteFoodtypesIds", required = true) Set<Integer> deleteFoodtypesIds) {
		ModelAndView mav = new ModelAndView();

		Set<Foodtype> deleteFoodtypes = foodtypeDAO.findFoodtypesByIds(deleteFoodtypesIds);
		Set<Foodtype> referencedFoodtypes = foodtypeService.loadReferenceFoodtypes(deleteFoodtypes);
		boolean hasReferenced;
		if (referencedFoodtypes.size() == 0) {
			mav.addObject("deleteEntitys", deleteFoodtypes);
			hasReferenced = false;
		} else {
			deleteFoodtypes.removeAll(referencedFoodtypes);
			mav.addObject("referencedEntitys", referencedFoodtypes);
			mav.addObject("deleteEntitys", deleteFoodtypes);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Foodtype");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listFoodtypes");
		return mav;
	}

	@RequestMapping("/deleteFoodtype")
	public String deleteFoodtype(@RequestParam Integer foodtypeId) {
		Foodtype foodtype = foodtypeDAO.findFoodtypeByPrimaryKey(foodtypeId);
		foodtypeService.deleteFoodtype(foodtype);
		return "forward:/listFoodtypes";
	}

	@RequestMapping("/confirmDeleteFoodtype")
	public ModelAndView confirmDeleteFoodtype(@RequestParam Integer foodtypeId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("foodtype", foodtypeDAO.findFoodtypeByPrimaryKey(foodtypeId));

		mav.setViewName("foodtype/deleteFoodtype.jsp");

		return mav;
	}

	@RequestMapping("/deleteFoodtypes")
	public String deleteFoodtypes(
			@RequestParam(value = "deleteFoodtypesId", required = true) Set<Integer> deleteFoodtypesId) {
		foodtypeService.deleteFoodtypes(deleteFoodtypesId);
		return "forward:/listFoodtypes";
	}

	@RequestMapping("/editFoodtype")
	public ModelAndView editFoodtype(@RequestParam Integer foodtypeId) {

		ModelAndView mav = new ModelAndView();

		Foodtype foodtype = foodtypeDAO.findFoodtypeByPrimaryKey(foodtypeId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("foodtype", foodtype);

		// 把所有的引用foodtype对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(foodtypeService.loadFoodtypeObjectReferencedSet(foodtype));

		// 将编辑页面foodtype添加到mav中
		mav.setViewName("foodtype/editFoodtype.jsp");
		return mav;
	}

	@RequestMapping("/newFoodtype")
	public ModelAndView newFoodtype() {
		ModelAndView mav = new ModelAndView();
		Foodtype foodtype = new Foodtype();

		mav.addObject("foodtype", foodtype);

		// 把所有的引用foodtype对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(foodtypeService.loadFoodtypeObjectReferencedSet(foodtype));

		mav.addObject("newFlag", true);
		mav.setViewName("foodtype/editFoodtype.jsp");

		return mav;
	}

	@RequestMapping("/saveFoodtype")
	public ModelAndView saveFoodtype(@ModelAttribute Foodtype foodtype) {

		ModelAndView mav = new ModelAndView();

		Foodtype parentTemp = foodtype.getParent();
		Foodtype parent = parentTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(foodtypeDAO.findFoodtypeById(parentTemp.getId()));

		Foodtype existingFoodtype = CmiHibernateProxyUtil
				.deproxy(foodtypeDAO.findFoodtypeByPhysicalPrimaryKey(foodtype.getName(), parent.getId()));

		foodtype.setParent(parent);
		// 新建记录
		if (foodtype.getId() == null) {
			if (foodtype.equals(existingFoodtype)) {

				mav.addAllObjects(foodtypeService.loadFoodtypeClassReferencedSet(foodtype));
				// mav = editContinue(mav, foodtype);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("foodtype/editFoodtype.jsp");
			} else {
				foodtypeService.saveFoodtype(foodtype);
				mav.setViewName("forward:/listFoodtypes");
			}
		}
		// 编辑记录
		else {
			Foodtype originFoodtype = CmiHibernateProxyUtil
					.deproxy(foodtypeDAO.findFoodtypeByPrimaryKey(foodtype.getId()));
			if (!foodtype.equals(originFoodtype) && foodtype.equals(existingFoodtype)) {
				mav.addAllObjects(foodtypeService.loadFoodtypeClassReferencedSet(foodtype));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("foodtype/editFoodtype.jsp");
			} else {
				foodtypeService.saveFoodtype(foodtype);
				mav.setViewName("forward:/listFoodtypes");
			}
		}
		return mav;
	}

	@RequestMapping("/foodtypeController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
